package jp.sourceforge.akjrcp.ui;





import java.io.File;
import java.io.IOException;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;

import org.eclipse.ui.PlatformUI;

public class DirectoryPathCombo extends Composite implements IPropertyChangeListener{
private IPreferenceStore store;
private String key;
private CCombo directoryPaths;
private SelectionListener selectionListener;
Button openButton;
Button explorerButton;
	public void setEnabled(boolean bool){
		directoryPaths.setEnabled(bool);
		if(openButton!=null)
		openButton.setEnabled(bool);
		
		if(explorerButton!=null)
			explorerButton.setEnabled(bool);
	}
	public File getSelectedDirectory(){
		String item=directoryPaths.getItem(directoryPaths.getSelectionIndex());
		if(item.length()>0){
			return new File(item);
		}
		return null;
	}
	public DirectoryPathCombo(Composite parent, int style,IPreferenceStore store,String key,Image openIcon,Image explorerIcon) {
		super(parent, style);
		this.store=store;
		this.key=key;
		this.setLayout(new GridLayout(3,false));
		
		if(openIcon!=null){
			openButton=new Button(this,SWT.NULL);
			openButton.setImage(openIcon);
			openButton.setToolTipText(Messages.getString("select_directory"));
			openButton.addSelectionListener(new SelectionListener(){

				public void widgetDefaultSelected(SelectionEvent e) {
					
				}

				public void widgetSelected(SelectionEvent e) {
					openDirectory();
				}

				
				
			});
		}
		
		directoryPaths=new CCombo(this,SWT.NULL);
		directoryPaths.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		updatePaths();
		
		if(explorerIcon!=null){
			explorerButton=new Button(this,SWT.NULL);
			explorerButton.setToolTipText(Messages.getString("show_directory"));
			explorerButton.setImage(explorerIcon);
			explorerButton.addSelectionListener(new SelectionListener(){

				public void widgetDefaultSelected(SelectionEvent e) {
					
				}

				public void widgetSelected(SelectionEvent e) {
					explorerDirectory();
				}

				

				
				
			});
		}
		store.addPropertyChangeListener(this);
	}
	

	public void explorerDirectory() {
		File file=getSelectedDirectory();
		if(file!=null){
		Runtime r=Runtime.getRuntime();
		try {
			r.exec(new String[]{"explorer",file.getAbsolutePath()});
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		}
	}
	
	public void openDirectory() {
		DirectoryDialog dialog=new DirectoryDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
		String path=dialog.open();
		if(path!=null){
			openDirectory(path);
		}
	}
	public void openDirectory(String path) {
		boolean find=false;
		
		for (int i = 0; i < directoryPaths.getItemCount(); i++) {
			if(directoryPaths.getItem(i).equals(path)){
				directoryPaths.select(i);
				find=true;
				break;
			}
		}
		if(!find){
			directoryPaths.add(path);
			directoryPaths.select(directoryPaths.getItemCount()-1);//select newone
			}
		
	}


	/*
 * (non-Javadoc)
 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
 * this called when property changed in a setting page
 */
	public void propertyChange(PropertyChangeEvent event) {
		updatePaths();
	}
	
	public void updatePaths() {
		String paths=store.getString(key);
		String path[]=paths.split(";");//one day should change for other platform.
		String current=null;
		if(directoryPaths.getSelectionIndex()>-1){
			current=directoryPaths.getItem(directoryPaths.getSelectionIndex());
		}
		directoryPaths.removeAll();
		
		for (int i = 0; i < path.length; i++) {
			directoryPaths.add(path[i]);
		}
		
		if(selectionListener!=null){
			directoryPaths.removeSelectionListener(selectionListener);
		}
		
		if(current==null){
			directoryPaths.select(0);
		}else{
			boolean find=false;
			for (int i = 0; i < path.length; i++) {
				if(path[i].equals(current)){
					directoryPaths.select(i);
					find=true;
					break;
				}
			}
			if(!find){
				if(current!=null){//something wrong?
				directoryPaths.add(current);
				directoryPaths.select(directoryPaths.getItemCount()-1);//select newone
				}
			}
		}
		if(selectionListener!=null){
			directoryPaths.addSelectionListener(selectionListener);
		}
		
		
	}


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	public SelectionListener getSelectionListener() {
		return selectionListener;
	}


	public void setSelectionListener(SelectionListener selectionListener) {
		this.selectionListener = selectionListener;
	}


	public IPreferenceStore getStore() {
		return store;
	}


	public void setStore(IPreferenceStore store) {
		this.store = store;
	}

	
}
